package com.juzipi.demo.ocp.improve;

public class Ocp {

    public static void main(String[] args) {
        //使用看看存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
        graphicEditor.drawShape(new OtherGraphic());
    }

}

//这是一个用于绘图的类
//这是使用方
class GraphicEditor{
    //接收Shape对象，调用 draw 方法
    public void drawShape(Shape shape){
        shape.draw();
    }

}

//Shaper基类
abstract class Shape{
    int my_type;

    public abstract void draw();//抽象方法
}

class Rectangle extends Shape {
    Rectangle(){
        super.my_type = 1;
    }

    @Override
    public void draw() {
        System.out.println("绘制了矩形");
    }
}

class Circle extends Shape {
    Circle() {
        super.my_type = 2;
    }

    @Override
    public void draw() {
        System.out.println("绘制了圆形");
    }
}

//新增画三角形
class Triangle extends Shape {
    Triangle(){
        super.my_type = 3;
    }

    @Override
    public void draw() {
        System.out.println("绘制了三角形");
    }
}

//新增一个图形
class OtherGraphic extends Shape {
    OtherGraphic(){
        super.my_type = 4;
    }

    @Override
    public void draw() {
        System.out.println("绘制了其他图形");
    }
}